/* 
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.ngrinder.perftest.repository;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;

import org.ngrinder.model.PerfTest;
import org.ngrinder.model.Status;
import org.ngrinder.model.User;
import org.springframework.data.jpa.domain.Specification;

/**
<<<<<<< HEAD
 * {@link PerfTest} Specification for the more elaborated search.
 * 
=======
 * {@link PerfTest} Specification for the more elaborated PerfTest query.
 *
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
 * @author JunHo Yoon
 * @since 3.0
 */
public abstract class PerfTestSpecification {

	/**
	 * Get the{@link Specification} checking if the {@link PerfTest} has one of given {@link Status}.
<<<<<<< HEAD
	 * 
	 * @param statuses
	 *            status set
=======
	 *
	 * @param statuses status set
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
	 * @return {@link Specification}
	 */
	public static Specification<PerfTest> statusSetEqual(final Status... statuses) {
		return new Specification<PerfTest>() {
			@Override
			public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				return root.get("status").in((Object[]) statuses);
			}
		};
	}

	/**
	 * Get the {@link Specification} checking if the {@link PerfTest#getTags()} has the given tagValue.
<<<<<<< HEAD
	 * 
	 * @param tagValue
	 *            tagValue
=======
	 *
	 * @param tagValue tagValue
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
	 * @return {@link Specification}
	 */
	public static Specification<PerfTest> hasTag(final String tagValue) {
		return new Specification<PerfTest>() {
			@Override
			public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				SetJoin<Object, Object> join = root.joinSet("tags");
				return cb.equal(join.get("tagValue"), tagValue);
			}
		};
	}

	/**
	 * Get the {@link Specification} checking if the {@link PerfTest} has one of the given IDs.
<<<<<<< HEAD
	 * 
	 * @param ids
	 *            id list
=======
	 *
	 * @param ids id list
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
	 * @return {@link Specification}
	 */
	public static Specification<PerfTest> idSetEqual(final Long[] ids) {
		return new Specification<PerfTest>() {
			@Override
			public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				return root.get("id").in((Object[]) ids);
			}
		};
	}

	/**
	 * Get the {@link Specification} checking if the {@link PerfTest} has the given ID.
<<<<<<< HEAD
	 * 
	 * @param id
	 *            perf test id
=======
	 *
	 * @param id perf test id
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
	 * @return {@link Specification}
	 */
	public static Specification<PerfTest> idEqual(final Long id) {
		return new Specification<PerfTest>() {
			@Override
			public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				return cb.equal(root.get("id"), id);
			}
		};
	}

	/**
	 * Get the {@link Specification} checking if the {@link PerfTest} has the given region.
<<<<<<< HEAD
	 * 
	 * @since 3.1
	 * @param region
	 *            region of perf test
	 * @return {@link Specification}
=======
	 *
	 * @param region region of perf test
	 * @return {@link Specification}
	 * @since 3.1
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
	 */
	public static Specification<PerfTest> idRegionEqual(final String region) {
		return new Specification<PerfTest>() {
			@Override
			public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
<<<<<<< HEAD
				return cb.equal(root.get("region"), region);
=======
				return cb.or(cb.equal(root.get("region"), region), cb.equal(root.get("region"), ""));
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
			}
		};
	}

	/**
	 * Get the {@link Specification} whichs provide empty predicate.
<<<<<<< HEAD
	 * 
	 * This is for the base element for "and" or "or" combination.
	 * 
=======
	 *
	 * This is for the base element for "and" or "or" combination.
	 *
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
	 * @return {@link Specification}
	 */
	public static Specification<PerfTest> idEmptyPredicate() {
		return new Specification<PerfTest>() {
			@Override
			public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				return root.get("id").isNotNull();
			}
		};
	}

	/**
	 * Get the Specification which provide empty predicate for schedule time. This is for the base element for "and" or
	 * "or" combination.
<<<<<<< HEAD
	 * 
=======
	 *
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
	 * @return {@link Specification}
	 */
	public static Specification<PerfTest> scheduledTimeNotEmptyPredicate() {
		return new Specification<PerfTest>() {
			@Override
			public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				return root.get("scheduledTime").isNotNull();
			}
		};
	}

	/**
	 * Get the search {@link Specification} for test name and description fields.
<<<<<<< HEAD
	 * 
	 * @param queryString
	 *            query String
=======
	 *
	 * @param queryString query String
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
	 * @return {@link Specification}
	 */
	public static Specification<PerfTest> likeTestNameOrDescription(final String queryString) {
		return new Specification<PerfTest>() {
			@Override
			public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				String queryStr = ("%" + queryString + "%").toLowerCase();
				return cb.or(cb.like(cb.lower(root.get("testName").as(String.class)), queryStr),
						cb.like(root.get("description").as(String.class), queryStr));
			}
		};
	}

	/**
	 * Get createBy {@link Specification} to get the {@link PerfTest}s whose creator is the given user.
<<<<<<< HEAD
	 * 
	 * @param user
	 *            user
=======
	 *
	 * @param user user
>>>>>>> 1b78002dcaa5e3c02ccbc6da93c1b0593631c3ad
	 * @return {@link Specification}
	 */
	public static Specification<PerfTest> createdBy(final User user) {
		return new Specification<PerfTest>() {
			@Override
			public Predicate toPredicate(Root<PerfTest> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				return cb.or(cb.equal(root.get("createdUser"), user));
			}
		};
	}

}
